package com.mediacloud.app.newsmodule.fragment.baoliao.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.pictureselector.GlideEngine;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.activity.VideoPreActivity;
import com.mediacloud.app.newsmodule.fragment.baoliao.OnDeleteListener;
import com.mediacloud.app.newsmodule.fragment.baoliao.model.LocalMediaWithState;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yoake on 2018/7/25.
 */

public class BaoLiaoAdapter extends RecyclerView.Adapter<BaoLiaoAdapter.BaoLiaoHolder> implements View.OnClickListener {
    private final Context context;
    public List<LocalMediaWithState> selectList;
    private final OnDeleteListener listener;
    private final List<LocalMedia> imageList;
    private long imgMaxSize;
    private long videoMaxSize;

    public BaoLiaoAdapter(Context context, OnDeleteListener listener) {
        this.context = context;
        this.listener = listener;
        this.selectList = new ArrayList<>();
        this.imageList = new ArrayList<>();
    }

    public void setImgMaxSize(long imgMaxSize) {
        this.imgMaxSize = imgMaxSize;
    }

    public void setVideoMaxSize(long videoMaxSize) {
        this.videoMaxSize = videoMaxSize;
    }

    public List<LocalMediaWithState> getSelectList() {
        return selectList;
    }

    public void setSelectList(List<LocalMediaWithState> selectList) {
        this.selectList.clear();
        this.selectList.addAll(selectList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaoLiaoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BaoLiaoHolder(LayoutInflater.from(context).inflate(R.layout.baoliao_media_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BaoLiaoHolder holder, int position) {
        LocalMedia media = selectList.get(position).getLocalMedia();

        if (PictureMimeType.getMimeType(media.getMimeType()) == PictureConfig.TYPE_VIDEO) {
            Glide.with(context).load(media.getPath()).into(holder.image);
            holder.videoTag.setVisibility(View.VISIBLE);
        } else if (PictureMimeType.getMimeType(media.getMimeType()) == PictureConfig.TYPE_AUDIO) {
            Glide.with(context).load(media.getPath()).into(holder.image);
            holder.videoTag.setVisibility(View.GONE);
        } else {
            holder.videoTag.setVisibility(View.GONE);
            if (media.isCompressed()) {
                Glide.with(context).load(media.getCompressPath()).into(holder.image);
            } else {
                Glide.with(context).load(media.getPath()).into(holder.image);
            }
        }
        File fl = new File(media.getRealPath());
        if (fl.length() > ((PictureMimeType.getMimeType(media.getMimeType()) == PictureConfig.TYPE_VIDEO) ? videoMaxSize : imgMaxSize)) {
            holder.imageLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_baoliao_img_2));
        } else {
            holder.imageLayout.setBackground(null);
        }
        holder.delete.setTag(position);
        holder.delete.setOnClickListener(this);
        holder.itemView.setOnClickListener(v -> {
            if (PictureMimeType.getMimeType(media.getMimeType()) == PictureConfig.TYPE_IMAGE) {
//                previewImage(position);
                listener.onPreImages(position);
            } else if (PictureMimeType.getMimeType(media.getMimeType()) == PictureConfig.TYPE_VIDEO) {
                previewVideo(media);
            }
        });
        int progress = selectList.get(position).getProgress();
        if (progress > 0) {
            holder.progressBar.setVisibility(View.VISIBLE);
            holder.progressBar.setProgress(progress);
        } else {
            holder.progressBar.setVisibility(View.GONE);
            holder.progressBar.setProgress(0);
        }

    }

    public List<LocalMedia> getImagesList(){
        imageList.clear();
        List<LocalMedia> list = new ArrayList<>();
        for (LocalMediaWithState localMediaWithState : getSelectList()) {
            if (PictureMimeType.getMimeType(localMediaWithState.getLocalMedia().getMimeType()) == PictureConfig.TYPE_IMAGE) {
                imageList.add(localMediaWithState.getLocalMedia());
            }
            list.add(localMediaWithState.getLocalMedia());
        }
        return imageList;
    }
    /**
     * 预览图片
     */
    private void previewImage(int position) {
        try {
            imageList.clear();
            List<LocalMedia> list = new ArrayList<>();
            for (LocalMediaWithState localMediaWithState : getSelectList()) {
                if (PictureMimeType.getMimeType(localMediaWithState.getLocalMedia().getMimeType()) == PictureConfig.TYPE_IMAGE) {
                    imageList.add(localMediaWithState.getLocalMedia());
                }
                list.add(localMediaWithState.getLocalMedia());
            }
            if (imageList != null && imageList.size() > 0) {
                PictureSelector.create((Activity) context)
                        .themeStyle(R.style.picture_default_style) // xml设置主题
                        .isNotPreviewDownload(false) // 预览图片长按是否可以下载
                        .imageEngine(GlideEngine.createGlideEngine()) // 外部传入图片加载引擎，必传项
                        .openExternalPreview(imageList.indexOf(list.get(position)), imageList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 预览视频
     */
    private void previewVideo(LocalMedia media) {
        try {
            if (media != null) {
                String videoUrl;
                if (TextUtils.isEmpty(media.getAndroidQToPath())) {
                    videoUrl = media.getRealPath();
                } else {
                    videoUrl = media.getAndroidQToPath();
                }
                Intent intent = new Intent(context, VideoPreActivity.class);
                intent.putExtra("URL", videoUrl);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return selectList.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        LocalMediaWithState localMediaWithState = selectList.remove(position);
        notifyDataSetChanged();
        listener.onDelete(localMediaWithState);
    }



    class BaoLiaoHolder extends RecyclerView.ViewHolder {

        ImageView image, videoTag, delete;
        ProgressBar progressBar;
        RelativeLayout imageLayout;

        public BaoLiaoHolder(View itemView) {
            super(itemView);
            imageLayout = itemView.findViewById(R.id.imageLayout);
            image = itemView.findViewById(R.id.image);
            videoTag = itemView.findViewById(R.id.videoTag);
            delete = itemView.findViewById(R.id.delete);
            progressBar = itemView.findViewById(R.id.progress_bar_h);
        }
    }
}